

package com.hazelcast.internal.memory;

import com.hazelcast.memory.Capacity;

import static com.hazelcast.internal.memory.MemoryStatsSupport.freePhysicalMemory;
import static com.hazelcast.internal.memory.MemoryStatsSupport.totalPhysicalMemory;

/**
 * Default implementation of MemoryStats.
 */
public class DefaultMemoryStats implements MemoryStats {

    private final Runtime runtime = Runtime.getRuntime();
    private final DefaultGarbageCollectorStats gcStats = new DefaultGarbageCollectorStats();

    @Override
    public final long getTotalPhysical() {
        return totalPhysicalMemory();
    }

    @Override
    public final long getFreePhysical() {
        return freePhysicalMemory();
    }

    @Override
    public final long getMaxHeap() {
        return runtime.maxMemory();
    }

    @Override
    public final long getCommittedHeap() {
        return runtime.totalMemory();
    }

    @Override
    public final long getUsedHeap() {
        return runtime.totalMemory() - runtime.freeMemory();
    }

    @Override
    public final long getFreeHeap() {
        return runtime.freeMemory();
    }

    @Override
    public long getMaxNative() {
        return 0;
    }

    @Override
    public long getCommittedNative() {
        return 0;
    }

    @Override
    public long getUsedNative() {
        return 0;
    }

    @Override
    public long getFreeNative() {
        return 0;
    }

    @Override
    public long getMaxMetadata() {
        return 0;
    }

    @Override
    public long getUsedMetadata() {
        return 0;
    }

    @Override
    public GarbageCollectorStats getGCStats() {
        GCStatsSupport.fill(gcStats);
        return gcStats;
    }

    @Override
    public String toString() {
        return "MemoryStats{" + "Total Physical: " + Capacity.toPrettyString(getTotalPhysical()) + ", Free Physical: " + Capacity.toPrettyString(getFreePhysical()) + ", Max Heap: " + Capacity.toPrettyString(getMaxHeap()) + ", Committed Heap: " + Capacity.toPrettyString(getCommittedHeap()) + ", Used Heap: " + Capacity.toPrettyString(getUsedHeap()) + ", Free Heap: " + Capacity.toPrettyString(getFreeHeap()) + ", " + getGCStats() + '}';
    }
}
